﻿#region [ License information          ]
/* ************************************************************
 *
 * Copyright (c) Bjørn Bouet Smith, 2010, 2011
 *
 * This source code is subject to terms and conditions of 
 * Microsoft Public License (Ms-PL).
 * 
 * A copy of the license can be found in the license.txt
 * file at the root of this distribution. If you can not 
 * locate the License, please send an email to bjornsmith@gmail.com
 * 
 * By using this source code in any fashion, you are 
 * agreeing to be bound by the terms of the Microsoft 
 * Permissive License.
 *
 * You must not remove this notice, or any other, from this
 * software.
 *
 * ************************************************************/
#endregion

using System.Configuration;

namespace Smith.AMC.Configuration
{
    /// <summary>
    /// Represents a collection of memcached server nodes.
    /// </summary>
    public class MemcachedNodeCollection : ConfigurationElementCollection
    {
        /// <summary>
        /// Gets the type of the <see cref="T:System.Configuration.ConfigurationElementCollection"/>.
        /// </summary>
        /// <returns>The <see cref="T:System.Configuration.ConfigurationElementCollectionType"/> of this collection.</returns>
        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.AddRemoveClearMap;
            }
        }

        /// <summary>
        /// Gets or sets the <see cref="MemcachedServerNodeConfiguration"/> at the specified index.
        /// </summary>
        /// <param name="index">The index of the item to get or set</param>
        public MemcachedServerNodeConfiguration this[int index]
        {
            get
            {
                return (MemcachedServerNodeConfiguration)BaseGet(index);
            }

            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }

                BaseAdd(index, value);
            }
        }

        /// <summary>
        /// Gets the <see cref="MemcachedServerNodeConfiguration"/> with the specified name.
        /// </summary>
        /// <param name="name">The name of the element to get.</param>
        public new MemcachedServerNodeConfiguration this[string name]
        {
            get
            {
                return (MemcachedServerNodeConfiguration)BaseGet(name);
            }
        }

        /// <summary>
        /// Returns the index of the given element.
        /// </summary>
        /// <param name="element">The element to find the index for.</param>
        /// <returns>The index of the given element</returns>
        public int IndexOf(MemcachedServerNodeConfiguration element)
        {
            return BaseIndexOf(element);
        }

        /// <summary>
        /// Adds the specified element.
        /// </summary>
        /// <param name="element">The element.</param>
        public void Add(MemcachedServerNodeConfiguration element)
        {
            BaseAdd(element);
        }

        /// <summary>
        /// Clears this instance.
        /// </summary>
        public void Clear()
        {
            BaseClear();
        }

        /// <summary>
        /// Removes the specified element.
        /// </summary>
        /// <param name="element">The element to remove.</param>
        public void Remove(MemcachedServerNodeConfiguration element)
        {
            BaseRemove(GetElementKeyValue(element));
        }

        /// <summary>
        /// Removes the element with the specified name.
        /// </summary>
        /// <param name="name">The name of the element to remove.</param>
        public void Remove(string name)
        {
            BaseRemove(name);
        }

        /// <summary>
        /// Removes the element at the given index
        /// </summary>
        /// <param name="index">The index to remove at.</param>
        public void RemoveAt(int index)
        {
            BaseRemoveAt(index);
        }

        /// <summary>
        /// Gets the element key for a specified configuration element when overridden in a derived class.
        /// </summary>
        /// <param name="element">The <see cref="T:System.Configuration.ConfigurationElement"/> to return the key for.</param>
        /// <returns>
        /// An <see cref="T:System.Object"/> that acts as the key for the specified <see cref="T:System.Configuration.ConfigurationElement"/>.
        /// </returns>
        protected override object GetElementKey(ConfigurationElement element)
        {
            MemcachedServerNodeConfiguration node = (MemcachedServerNodeConfiguration)element;
            return GetElementKeyValue(node);
        }

        /// <summary>
        /// When overridden in a derived class, creates a new <see cref="T:System.Configuration.ConfigurationElement"/>.
        /// </summary>
        /// <returns>
        /// A new <see cref="T:System.Configuration.ConfigurationElement"/>.
        /// </returns>
        protected override ConfigurationElement CreateNewElement()
        {
            return new MemcachedServerNodeConfiguration();
        }

        /// <summary>
        /// Gets the element key value, used for looking up elements
        /// </summary>
        /// <param name="node">The node to get a key for.</param>
        /// <returns>The value to be used for look ups</returns>
        private static string GetElementKeyValue(MemcachedServerNodeConfiguration node)
        {
            return string.Format("{0}:{1}", node.HostName, node.TcpPortNumber);
        }
    }
}
